import req from '@/utils/request';
import * as URL from '@/constants/api';

export function getArticleDetail (id) {
  return req({
    url: URL.ARTICLE_DETAIL + id,
  })
  .then(res => res.data.data);
}

export function follow (id) {
  return req({
    url: URL.FOLLOW,
    method: 'post',
    data: {
      target: id,
    },
  })
  .then(res => res.data.message);
}

export function unfollow (id) {
  return req({
    url: URL.UNFOLLOW + id,
    method: 'delete',
  })
  .then(res => res.data.message || '取消关注成功');
}

// 点赞文章
export function likeArticle (id) {
  return req({
    url: URL.LIKE_ARTICLE,
    method: 'post',
    data: {
      target: id,
    },
  })
  .then(res => res.data.message);
}

// 取消点赞文章
export function unlikeArticle (id) {
  return req({
    url: URL.UNLIKE_ARTICLE + id,
    method: 'delete',
  })
  .then(res => res.data.message || '取消点赞成功');
}

// 点赞评论
export function likeComment (id) {
  return req({
    url: URL.LIKE_COMMENT,
    method: 'post',
    data: {
      target: id,
    },
  })
  .then(res => res.data.message);
}

// 取消点赞评论
export function unlikeComment (id) {
  return req({
    url: URL.UNLIKE_COMMENT + id,
    method: 'delete',
  })
  .then(res => res.data.message || '取消点赞成功');
}

// 收藏文章
export function collectArticle (id) {
  return req({
    url: URL.COLLECT_ARTICLE,
    method: 'post',
    data: {
      target: id,
    },
  })
  .then(res => res.data.message);
}

// 取消收藏文章
export function cancelCollectArticle (id) {
  return req({
    url: URL.CANCEL_COLLECT_ARTICLE + id,
    method: 'delete',
  })
  .then(res => res.data.message || '取消收藏成功');
}

// 评论文章
export function commentArticle (id, content) {
  return req({
    url: URL.COMMENT,
    method: 'post',
    data: {
      target: id,
      content,
      // art_id
    },
  })
  .then(res => res.data.message || '评论成功');
}

// 获取评论
export function getCommentList(id) {
  return req({
    url: URL.COMMENT,
    params: {
      source: id,
      type: 'a',
    },
  })
  .then(res => res.data.data.results);
}
